<?php
/**
 * CONTENIDO extension pack (mpContenidoX), consists of multiple classes to
 * provide more features for Plugins/Modules.
 *
 * @package     mpContenidoX
 * @subpackage  VariableStorage
 * @author      Murat Purc <murat@purc.de>
 * @copyright   Copyright (c) 2008-2012 Murat Purc (http://www.purc.de)
 * @license     http://www.gnu.org/licenses/gpl-2.0.html - GNU General Public License, version 2
 * @version     $Id: Abstract.class.php 21 2012-07-16 21:33:14Z purcaholic $
 */


defined('CON_FRAMEWORK') or die('Illegal call');


/**
 * Abstract base config class. Extended classes must overwrite existing Methods.
 *
 * @package     mpContenidoX
 * @subpackage  VariableStorage
 * @author      Murat Purc <murat@purc.de>
 */
abstract class mpVariableStorage_Abstract
{

    /**
     * Flag about occured error
     * @var  bool
     */
    protected $_bError = false;

    /**
     * Key of configuration (could be a file or a md5 hash, depends on child class)
     * @var  string
     */
    protected $_key = null;

    /**
     * Lifetime of cached data in seconds
     * @var  int
     */
    protected $_lifetime = 0;


    /**
     * Constructor of mpVariableStorage_Abstract, does some checks, stops further script execution if validation of
     * arguments fails.
     *
     * @param  array  $options  Assoziative options array as follows:
     * <code>
     * $options['key']      = Configuration key, depending on child class
     * $options['lifetime'] = Lifetime of data in seconds, optional
     * </code>
     * @throws  InvalidArgumentException If option key 'key' is missing or empty
     */
    public function __construct(array $options)
    {
        if (!isset($options['key'])) {
            throw new InvalidArgumentException('mpVariableStorage_Abstract->__construct: Missing argument $options["key"]');
        } elseif ((string) $options['key'] === '') {
            throw new InvalidArgumentException('mpVariableStorage_Abstract->__construct: Empty argument $options["key"]');
        }

        $this->_key = $options['key'];

        if (isset($options['lifetime']) && (int) $options['lifetime'] > 0) {
            $this->_lifetime = (int) $options['lifetime'];
        }
    }


    /**
     * Returns remained lifetime
     *
     * @return  mixed  Remained lifetime or null
     */
    protected function getExpires()
    {
        return ($this->_lifetime > 0) ? time() - $this->_lifetime : null;
    }

}
